package it.uniroma2.util.vector;

import it.uniroma2.util.math.ArrayMath;

import java.util.Random;

/**
 * @author Lorenzo Dell'Arciprete
 * This class generates random vectors for the tree node labels by using the label String hashcode
 * as the seed for the Random generator used to fetch the vector elements. This is used in conjunction
 * with a secondary seed that acts as a common offset.
 */
public class RandomVectorGenerator implements VectorProvider {
	private Random random;
	private int offset;
	private int vectorSize;
	
	public RandomVectorGenerator(int vectorSize) {
		this(vectorSize, 0);
	}
	
	public RandomVectorGenerator(int vectorSize, int offset) {
		random = new Random(offset);
		this.offset = offset;
		this.vectorSize = vectorSize;
	}
	
	public void setVectorSize(int vectorSize) {
		this.vectorSize = vectorSize;
	}
	
	public void setOffset(int offset) {
		this.offset = offset;
	}
	
	public void setRandomSeed(int seed) {
		random.setSeed(seed+offset);
	}
	
	public double[] generateRandomVector() { 
		double[] randomVector = new double[vectorSize];
		for (int i=0; i<vectorSize; i++)
			randomVector[i] = random.nextGaussian();
		try {
			randomVector = ArrayMath.versor(randomVector);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		return randomVector;
	}

	public double[] generateRandomVector(int seed) {
		setRandomSeed(seed);
		return generateRandomVector();
	}

	public double[] getVector(String term) {
		return generateRandomVector(term.hashCode());
	}

	public double[] generateZeroVector() {
		double[] v = new double[vectorSize];
		for (int i=0;i <vectorSize; i++) v[i] = 0.0;
		return v;
	}
	
}
